package com.zmc.third.controller;

import com.zmc.common.annotation.PreventRepeatSubmit;
import com.zmc.common.domain.freight.FreightCreateOrder;
import com.zmc.common.domain.freight.FreightQueryPrice;
import com.zmc.common.domain.resp.freight.FreightPriceResp;
import com.zmc.common.enums.RequestSourceEnum;
import com.zmc.common.utils.R;
import com.zmc.third.application.LogisticsService;
import com.zmc.third.infrastructure.bean.logistics.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author：zmc
 * @Date：2025/1/16 10:29
 */
@RestController
@RequestMapping("/freight")
public class LogisticsController {

    @Autowired
    private LogisticsService logisticsService;

    /**
     * 创建物流单
     * */
    @PreventRepeatSubmit(lockByPerson = true, source = RequestSourceEnum.MANAGE)
    @RequestMapping("/create")
    public R createOrder(@RequestBody FreightCreateOrder freightCreateOrder) {
        return R.ok(logisticsService.createOrder(freightCreateOrder));
    }

    /**
     * 查询当前订单配送价格
     * */
    @PostMapping("/query")
    public R freightQuery(@RequestBody FreightQueryPrice freightQueryPrice){
        PriceQuery query = new PriceQuery();
        query.setVolume(freightQueryPrice.getVolume());
        query.setWeight(freightQueryPrice.getWeight());
        DestAddress destAddress = new DestAddress();
        destAddress.setCode(freightQueryPrice.getDestPostCode());
        query.setDestAddress(destAddress);
        SrcAddress srcAddress = new SrcAddress();
        srcAddress.setCode(freightQueryPrice.getSrcPostCode());
        query.setSrcAddress(srcAddress);
        FreightPriceResp freightPriceResp = new FreightPriceResp(logisticsService.queryPrice(query));
        return R.ok(freightPriceResp);
    }

    /**
     * 取消物流订单
     * */
    @GetMapping("/cancel/{orderSn}")
    public R cancelFreight(@PathVariable("orderSn") String orderSn) {
        logisticsService.cancel(orderSn);
        return R.ok();
    }

    /**
     * 物流路由节点查询
     * */
    @RequestMapping(value = "/route/query", method = RequestMethod.POST)
    public R routeQuery(String orderSn) {
        return R.ok(logisticsService.routeQuery(orderSn));
    }

    /**
     * 路由推送接口
     * 使用内网穿透工具会被封掉，后期上云服务器再联调
     * */
    @RequestMapping(value="/callback", method = RequestMethod.POST)
    public @ResponseBody String callback(String content) {
        // 这里可以对接收到的content解析后进行操作
        /*ShunFengResponse response = XmlHelper.xmlToBeanForSF(content);*/
        return "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
                "<Response service=\"RoutePushService\">" +
                "<Head>OK</Head>" +
                "</Response>";
    }

}
